home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Mac Game Programming Gurus / TricksOfTheMacGameProgrammingGurus.iso / More Source / C⁄C++ / 3D Buttons CDEF 1.0b6 / Source / 3D Buttons Demo source / 3D Buttons Demo.π.cp < prev    next >
Text File  |  1994-07-29  |  8KB  |  322 lines

  1. /*
  2.     3D Buttons Demo
  3.     
  4.     Public Domain source by Zig Zichterman
  5.     ziggr@aol.com
  6.     
  7.     A real quick and dirty demo for the 3D Buttons CDEF.
  8.     
  9.     If you put CDEF 0 in the resource file ("3D Buttons Demo.π.rsrc") for
  10.     this project, the demo will use it for the dialog. I have already 
  11.     put version 1.0b1 of the 3D Buttons CDEF in the resource file.
  12.     
  13.     1.0b3
  14.         07/29/94    zz        make dialog movable. This code isn't Mixed Mode
  15.                                 savvy, 'cause I don't have a PowerPC yet.
  16.     1.0b2
  17.         07/20/94    zz        call InitCursor() right before running the dialog,
  18.     1.0b1    
  19.         06/29/94    zz        these comments    
  20.     
  21. */
  22.  
  23. void    InitializeToolbox();
  24. void    Toggle(DialogPtr inDialog, short inItemNum);
  25. void    Tristate(DialogPtr inDialog, short inItemNum);
  26. void    Disable(DialogPtr inDialog, short inItemNum);
  27. void    SetValue(DialogPtr inDialog, short inItemNum, short inValue);
  28. pascal Boolean    DragginFilter(DialogPtr inDialog, EventRecord *inEvent,
  29.                     short *ioItemHit);
  30. void        FlashButton(DialogPtr inDialog, short inItemNum);
  31. pascal void    DefaultFrameProc(DialogPtr inDialog, short inItem);
  32. void        SetupDefaultFrameProc(DialogPtr inDialog);
  33.  
  34. enum {
  35.     push_enabled    = 1,
  36.     check_enabled,
  37.     radio1_enabled,
  38.     radio2_enabled,
  39.     icon1_enabled,
  40.     icon2_enabled,
  41.     
  42.     push_disabled,
  43.     check_disabled,
  44.     radio1_disabled,
  45.     radio2_disabled,
  46.     icon1_disabled,
  47.     icon2_disabled,
  48.     
  49.     push_trireset,
  50.     
  51.     check_tristate,
  52.     radio1_tristate,
  53.     radio2_tristate,
  54.     
  55.     exit,
  56.     exitFrame
  57. };
  58.  
  59. void
  60. main(void)
  61. {
  62.     InitializeToolbox();
  63.     MaxApplZone();
  64.     
  65.     DialogPtr    dialog = ::GetNewDialog(128,NULL,(WindowPtr) -1);
  66.     if (!dialog) return;
  67.     
  68.     // set up dialog
  69.     SetValue(dialog,radio1_enabled,1);
  70.     SetValue(dialog,check_tristate,2);
  71.     SetValue(dialog,radio1_tristate,2);
  72.     SetValue(dialog,radio2_tristate,2);
  73.     SetValue(dialog,radio1_disabled,1);
  74.     
  75.     Disable(dialog,push_disabled);
  76.     Disable(dialog,check_disabled);
  77.     Disable(dialog,radio1_disabled);
  78.     Disable(dialog,radio2_disabled);
  79.     Disable(dialog,icon1_disabled);
  80.     Disable(dialog,icon2_disabled);
  81.  
  82.     SetupDefaultFrameProc(dialog);
  83.     
  84.     ::InitCursor();
  85.     short itemHit;
  86.     do {
  87.         ::ModalDialog(DragginFilter,&itemHit);
  88.         
  89.         // handle clicks
  90.         switch (itemHit) {
  91.             case check_enabled  :
  92.                 Toggle(dialog,itemHit);
  93.                 break;
  94.             case radio1_enabled  :
  95.             case radio1_tristate :
  96.                 SetValue(dialog,itemHit,1);
  97.                 SetValue(dialog,itemHit + 1,0);
  98.                 break;
  99.             case radio2_enabled  :
  100.             case radio2_tristate :
  101.                 SetValue(dialog,itemHit,1);
  102.                 SetValue(dialog,itemHit - 1,0);
  103.                 break;
  104.             case check_tristate :
  105.                 Tristate(dialog,itemHit);
  106.                 break;
  107.             case push_trireset  :
  108.                 SetValue(dialog,check_tristate,2);
  109.                 SetValue(dialog,radio1_tristate,2);
  110.                 SetValue(dialog,radio2_tristate,2);
  111.                 break;
  112.         }
  113.     } while (itemHit != exit);
  114.     
  115.     ::DisposeDialog(dialog);
  116. }
  117.  
  118. /**************************************************************************
  119.     Toggle()
  120.     
  121.     Toggle the value of a control item in a dialog
  122. **************************************************************************/
  123. void
  124. Toggle(DialogPtr inDialog, short inItemNum)
  125. {
  126.     short    iType;
  127.     Handle    iHandle;
  128.     Rect    iRect;
  129.     short    iValue;
  130.     
  131.     GetDItem(inDialog,inItemNum,&iType,&iHandle,&iRect);
  132.     if (iHandle) {
  133.         iValue = GetCtlValue((ControlHandle) iHandle);
  134.         iValue = !iValue;
  135.         SetCtlValue((ControlHandle) iHandle,iValue);
  136.     }
  137. }
  138.  
  139. /**************************************************************************
  140.     Disable()
  141.     
  142.     Disable (dim) a control item in a dialog
  143. **************************************************************************/
  144. void
  145. Disable(DialogPtr inDialog, short inItemNum)
  146. {
  147.     short    iType;
  148.     Handle    iHandle;
  149.     Rect    iRect;
  150.     short    iValue;
  151.     
  152.     GetDItem(inDialog,inItemNum,&iType,&iHandle,&iRect);
  153.     iType |= itemDisable;
  154.     SetDItem(inDialog,inItemNum,iType,iHandle,&iRect);
  155.     
  156.     if (iType & ctrlItem) {
  157.         HiliteControl((ControlHandle) iHandle,255);
  158.     }
  159. }
  160.  
  161. /**************************************************************************
  162.     Tristate()
  163.     
  164.     Rotate a control value through 0,1 and 2 (2 = tristate)
  165. **************************************************************************/
  166. void
  167. Tristate(DialogPtr inDialog, short inItemNum)
  168. {
  169.     short    iType;
  170.     Handle    iHandle;
  171.     Rect    iRect;
  172.     short    iValue;
  173.     
  174.     GetDItem(inDialog,inItemNum,&iType,&iHandle,&iRect);
  175.     if (iHandle) {
  176.         iValue = GetCtlValue((ControlHandle) iHandle);
  177.         if (++iValue > 2) iValue = 0;
  178.         SetCtlValue((ControlHandle) iHandle,iValue);
  179.     }
  180. }
  181.  
  182. /**************************************************************************
  183.     SetValue()
  184.     
  185.     Set the value of a control
  186. **************************************************************************/
  187. void
  188. SetValue(DialogPtr inDialog, short inItemNum, short inValue)
  189. {
  190.     short    iType;
  191.     Handle    iHandle;
  192.     Rect    iRect;
  193.     
  194.     GetDItem(inDialog,inItemNum,&iType,&iHandle,&iRect);
  195.     if (iHandle) {
  196.         short max = GetCtlMax((ControlHandle) iHandle);
  197.         if (max < inValue) SetCtlMax((ControlHandle) iHandle, inValue);
  198.         SetCtlValue((ControlHandle) iHandle,inValue);
  199.     }
  200. }
  201.  
  202.  
  203. /**************************************************************************
  204.     InitializeToolbox()
  205.     
  206.     The requisite toolbox initialization that every application under
  207.     the sun has.
  208. **************************************************************************/
  209. void
  210. InitializeToolbox()
  211. {
  212.     InitGraf((Ptr) &qd.thePort);        // Toolbox Managers
  213.     InitFonts();
  214.     InitWindows();
  215.     InitMenus();
  216.     TEInit();
  217.     InitDialogs(nil);
  218. }
  219.  
  220. /**************************************************************************
  221.     DragginFilter()
  222.     
  223.     My dialog filter proc that makes the modal dialog movable
  224. **************************************************************************/
  225. pascal Boolean
  226. DragginFilter(DialogPtr inDialog, EventRecord *inEvent,    short *ioItemHit)
  227. {
  228.     Boolean    done    = false;
  229.     
  230.     switch (inEvent->what) {
  231.         case mouseDown :
  232.             {
  233.                 WindowPtr    hitWindow;
  234.                 short    hitPart = ::FindWindow(inEvent->where,&hitWindow);
  235.                 if ((hitPart == inDrag) && (hitWindow == inDialog)) {
  236.                     RgnHandle    greyRgn = GetGrayRgn();    // pin to desktop
  237.                     ::DragWindow(hitWindow,inEvent->where,&((**greyRgn).rgnBBox));
  238.                     done = true;
  239.                 }
  240.             } 
  241.             break;
  242.         case keyDown :
  243.             {
  244.                 char charTyped = inEvent->message & 0x00FF;
  245.                 switch (charTyped) {
  246.                     case 'q' :        // allow cmd-Q, just for kicks
  247.                         if (!(inEvent->modifiers & cmdKey)) break;
  248.                         // allow fall-through only if command key was down
  249.                     case 0x03 :        // enter
  250.                     case 0x0D :        // return
  251.                         *ioItemHit = exit;
  252.                         done = true;
  253.                         FlashButton(inDialog,exit);
  254.                 }
  255.             }
  256.             break;
  257.     } 
  258.     
  259.     return done;
  260. }
  261.  
  262. /**************************************************************************
  263.     FlashButton()
  264.     
  265.     Flash a button for 8 ticks
  266. **************************************************************************/
  267. void
  268. FlashButton(DialogPtr inDialog, short inItem)
  269. {
  270.     short    iType;
  271.     Handle    iHandle;
  272.     Rect    iRect;
  273.     short    iValue;
  274.     
  275.     GetDItem(inDialog,inItem,&iType,&iHandle,&iRect);
  276.     if (iHandle) {
  277.         iValue = GetCtlValue((ControlHandle) iHandle);
  278.         iValue = !iValue;
  279.         ::HiliteControl((ControlHandle) iHandle,1);
  280.         { long junk; ::Delay(8,&junk); }
  281.         ::HiliteControl((ControlHandle) iHandle,0);
  282.     }
  283. }
  284.  
  285. /**************************************************************************
  286.     DefaultFrameProc
  287.     
  288.     Draw a frame around the exit button
  289. **************************************************************************/
  290. pascal void
  291. DefaultFrameProc(DialogPtr inDialog, short inItem)
  292. {
  293.     // ignore inItem, just draw a rect around exit button
  294.     short    iType;
  295.     Handle    iHandle;
  296.     Rect    iRect;
  297.     short    iValue;
  298.     
  299.     GetDItem(inDialog,exit,&iType,&iHandle,&iRect);
  300.     ::InsetRect(&iRect,-4,-4);
  301.     ::PenNormal();
  302.     ::PenSize(3,3);
  303.     ::FrameRoundRect(&iRect,16,16);
  304. }
  305.  
  306. /**************************************************************************
  307.     SetupDefaultFrameProc()
  308.     
  309.     Set exitFrame's draw function to DefaultFrameProc()
  310. **************************************************************************/
  311. void
  312. SetupDefaultFrameProc(DialogPtr inDialog)
  313. {
  314.     short    iType;
  315.     Handle    iHandle;
  316.     Rect    iRect;
  317.     short    iValue;
  318.     
  319.     GetDItem(inDialog,exitFrame,&iType,&iHandle,&iRect);
  320.     SetDItem(inDialog,exitFrame,iType,(Handle) DefaultFrameProc, &iRect);
  321. }
  322.